/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.Categoria;
import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.Usuario;
import edu.upc.clase.demo.service.CategoriaService;
import java.util.List;
import org.omg.CORBA.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author operador
 */
@Controller("categoriaController") 
public class CategoriaController {
    
    @Autowired
    private CategoriaService categoriaService;
    private static Logger log = LoggerFactory.getLogger(UsuarioController.class);

        @RequestMapping(value = "/planes/mante_cate", method = RequestMethod.GET)
    public ModelAndView manteCate() {
            ModelAndView mav = new ModelAndView("planes/mante_cate");
        List<Categoria> categorias = categoriaService.buscarTodos();
        //log.info("resultado = " + categoriaService.buscarTodos().size());
        mav.addObject("categorias", categorias);
        return mav;
    }   

    @RequestMapping("/categorias/lista")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("categorias/index");
        List<Categoria> categorias = categoriaService.buscarTodos();
        //log.info("resultado = " + categoriaService.buscarTodos().size());
        mav.addObject("categorias", categorias);
        return mav;
    }

    @RequestMapping(value="/categorias/edit", method=RequestMethod.GET)
    public ModelAndView editCategoria(@RequestParam("id")Integer id){
        ModelAndView mav = new ModelAndView("categorias/edit");
        Categoria c=categoriaService.buscar(id);
        mav.getModelMap().put("categoria", c);
        return mav;
    }

    @RequestMapping(value="/categorias/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("categoria") Categoria c, SessionStatus status)
    {
        categoriaService.actualizar(c);
        status.setComplete();
        return "redirect:/pages/categorias/lista";
    }

    

    @RequestMapping("/categorias/delete")
    public ModelAndView delete(@RequestParam("id")Integer id) {
        ModelAndView mav = new ModelAndView("redirect:/pages/categorias/lista");              
        
        return mav;
    }
            
    
}
